﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Prototype_Game_1
{
    class UserBasketSprite : Sprite
    {
        private List<Texture2D> mUserBasketSprties;
        private int currentBasketSpriteIndex = 1;
        private bool mOldPress = true;
        private bool mNewPress = false;
        public enum Texture { up, left, down, right };
        private Texture mCurrentTexture = Texture.up;

        public UserBasketSprite(string name, Texture2D textureImage, Vector2 position,
            Point size, int collisionOffset,
            Vector2 speed, List<Texture2D> userBasketSprties)
            : base(name, textureImage, position, size, collisionOffset, speed) 
        {
            mUserBasketSprties = userBasketSprties;
        }

        public Texture CurrentTexture
        {
            get { return mCurrentTexture; }
        }

        public override Vector2 direction
        {
            get
            {
                Vector2 inputDirection = Vector2.Zero;
                if (Keyboard.GetState( ).IsKeyDown(Keys.Left))
                    inputDirection.X -= 1;
                if (Keyboard.GetState( ).IsKeyDown(Keys.Right))
                    inputDirection.X += 1;
                if (Keyboard.GetState( ).IsKeyDown(Keys.Up))
                    inputDirection.Y -= 1;
                if (Keyboard.GetState( ).IsKeyDown(Keys.Down))
                    inputDirection.Y += 1;

                return inputDirection * speed;
            }
        }

        public override void Update(GameTime gameTime, Rectangle clientBounds)
        {
            // Move the sprite based on direction
            position += direction;

            if (Keyboard.GetState().IsKeyDown(Keys.RightAlt))
                mOldPress = true;

            if(Keyboard.GetState().IsKeyDown(Keys.RightAlt) && mOldPress == true && mNewPress == false)
            {
                mNewPress = true;
                textureImage = mUserBasketSprties.ElementAt(currentBasketSpriteIndex);
                mCurrentTexture++;
                currentBasketSpriteIndex++;
                if (currentBasketSpriteIndex == mUserBasketSprties.Count)
                    currentBasketSpriteIndex = 0;
                if (mCurrentTexture.ToString().Equals("4"))
                    mCurrentTexture = Texture.up;
            }

            if (Keyboard.GetState().IsKeyUp(Keys.RightAlt))
                mNewPress = false;

            mOldPress = mNewPress;

            // If sprite is off the screen, move it back within the game window
            if (position.X < 0)
                position.X = 0;
            if (position.Y < 0)
                position.Y = 0;
            if (position.X > clientBounds.Width - size.X)
                position.X = clientBounds.Width - size.X;
            if (position.Y > clientBounds.Height - size.Y)
                position.Y = clientBounds.Height - size.Y;

            base.Update(gameTime, clientBounds);
        }

        public override Rectangle collisionRect
        {
            get
            {
                return new Rectangle(
                (int)position.X + collisionOffset,
                (int)position.Y + collisionOffset,
                size.X - (collisionOffset * 2),
                size.Y - (collisionOffset * 2));
            }
        }  
    }
}
